#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "cxutil/utility.h"

int main(int OMIT_PARAM, char **OMIT_PARAM)
{
    int a = open("dir/a", O_RDWR|O_CREAT|O_EXCL|O_NOATIME, 0200);
    expect(-1 != a);
    struct stat st;
    expect(0 == stat("dir/a", &st));
    expect(1 == st.st_nlink);
    expect(0 == link("dir/a", "dir/b"));
    expect(0 == fstat(a, &st));
    expect(2 == st.st_nlink);
    expect(0 == unlink("dir/a"));
    expect(6 == pwrite(a, "hello", 6, 0x100000000));
    expect(0 == stat("dir/b", &st));
    expect_eq(1L, st.st_nlink, "%lu");
    expect(-1 == unlink("dir/a"));
    expect(ENOENT == errno);
    expect(0 == unlink("dir/b"));
    expect(0 == fstat(a, &st));
    expect_eq(0L, st.st_nlink, "%lu");
    expect(0x100000006 == st.st_size);
    char buf[6];
    expect(0x100000000 == lseek(a, 0x100000000, SEEK_SET));
    expect(6 == read(a, buf, 6));
    expect(0 == memcmp(buf, "hello", 6));
    expect(0 == close(a));
    return 0;
}
